package xgenerator.factory;

import xgenerator.constants.DbTypes;
import xgenerator.dao.DataSourceConfigDao;
import xgenerator.dao.MetadataDao;

/**
 * <p>
 * Title：抽象工厂DaoFactory
 * </p>
 * <p>
 * Description：
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see 
 * @since 1.0
 */
public abstract class DaoFactory implements DaoFactoryConfig, DbTypes {

	/**
	 * <p>
	 * Title：根据工厂dbType获取工厂实例
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dbType
	 * @return
	 */
	public static DaoFactory getInstance(String dbType) {
		return getInstance(mapFactoryClass(dbType));
	}
	
	/**
	 * <p>
	 * Title：根据工厂Class获取工厂实例
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param className
	 * @return
	 */
	public static DaoFactory getInstance(Class<? extends DaoFactory> clazz) {
		DaoFactory daoFactory = null;
		try {
			daoFactory = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return daoFactory;
	}

	/**
	 * <p>
	 * Title：获取DataSourceConfigDao
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public abstract DataSourceConfigDao getDataSourceConfigDao();

	/**
	 * <p>
	 * Title：获取MetadataDao
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public abstract MetadataDao getMetadataDao();
	
	/**
	 * <p>
	 * Title：将数据库类型名转换为DaoFactory.class
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dbType
	 * @return
	 */
	public static Class<? extends DaoFactory> mapFactoryClass(String dbType) {
		if(null == dbType || "".equals(dbType.trim())) {
			throw new RuntimeException("error dbType");
		}
		
		Class<? extends DaoFactory> daoFactoryClass = null;
		if(ORACLE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = ORACLE_CLASS;
		} else if(DB2_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = DB2_CLASS;
		} else if(SQL_SERVER_TYPE.equalsIgnoreCase(dbType) || -1 != dbType.toUpperCase().indexOf(SQL_SERVER_TYPE.toUpperCase())) {
			daoFactoryClass = SQL_SERVER_CLASS;
		} else if(SYBASE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = SYBASE_CLASS;
		} else if(INFORMIX_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = INFORMIX_CLASS;
		} else if(POSTGRESQL_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = POSTGRESQL_CLASS;
		} else if(MYSQL_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = MYSQL_CLASS;
		} else if(DERBY_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = DERBY_CLASS;
		} else if(SQLITE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = SQLITE_CLASS;
		} else if(ACCESS_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = ACCESS_CLASS;
		} else if(XML_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClass = XML_CLASS;
		}
		
		return daoFactoryClass;
	}
	
	/**
	 * <p>
	 * Title：将数据库类型名转换为DaoFactoryName
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param dbType
	 * @return
	 */
	public static String mapFactoryClassName(String dbType) {
		if(null == dbType || "".equals(dbType.trim())) {
			throw new RuntimeException("error dbType");
		}
		
		String daoFactoryClassName = null;
		if(ORACLE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = ORACLE;
		} else if(DB2_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = DB2;
		} else if(SQL_SERVER_TYPE.equalsIgnoreCase(dbType) || -1 != dbType.toUpperCase().indexOf(SQL_SERVER_TYPE.toUpperCase())) {
			daoFactoryClassName = SQL_SERVER;
		} else if(SYBASE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = SYBASE;
		} else if(INFORMIX_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = INFORMIX;
		} else if(POSTGRESQL_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = POSTGRESQL;
		} else if(MYSQL_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = MYSQL;
		} else if(DERBY_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = DERBY;
		} else if(SQLITE_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = SQLITE;
		} else if(ACCESS_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = ACCESS;
		} else if(XML_TYPE.equalsIgnoreCase(dbType)) {
			daoFactoryClassName = XML;
		}
		
		return daoFactoryClassName;
	}
	
}
